package com.mfoyou.agent.web.utils.withdraw;

import java.util.Map;

import org.mfoyou.agent.common.dao.domain.HuawuAgentWxInfo;
import org.mfoyou.agent.utils.wx.XMLParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mfoyou.agent.web.utils.wx.HttpRequest;
import com.mfoyou.agent.web.utils.wx.WxWithDrawData;

/**
 * 微信提现
 *
 * @author Administrator
 */
public class WechatWithDraw extends AbstractWithDraw {

    private String openId;

    private String userName;

    private HuawuAgentWxInfo huawuAgentWxInfo;

    private Integer amount;

    private String desc;

    public static final String withdrawUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

    private String payNumber;

    private Logger logger = LoggerFactory.getLogger(WechatWithDraw.class);

    public WechatWithDraw(String openId, String userName, HuawuAgentWxInfo huawuAgentWxInfo, Integer amount,
                          String desc, String payNumber) {
        super();
        this.openId = openId;
        this.userName = userName;
        this.huawuAgentWxInfo = huawuAgentWxInfo;
        this.amount = amount;
        this.desc = desc;
        this.payNumber = payNumber;
    }

    /**
     * 处理中
     */
    @Override
    public boolean dealWith() {
        WxWithDrawData wxWithDrawData = new WxWithDrawData(huawuAgentWxInfo.getXcxId(), huawuAgentWxInfo.getXcxMchid(),
                openId, payNumber, userName, amount, desc, huawuAgentWxInfo.getXcxPaySecret());
        HttpRequest httpRequest = new HttpRequest(huawuAgentWxInfo.getXcxPayRediPath(), huawuAgentWxInfo.getXcxMchid());
        String responseString = null;
        try {
            responseString = httpRequest.sendPost(withdrawUrl, wxWithDrawData);
        } catch (Exception e) {
            setMessage("微信参数设置错误");
            return false;
        }
        logger.info(responseString);
        Map<String, String> mapFromXML = null;
        try {
            mapFromXML = XMLParse.getMapFromXML(responseString);
        } catch (Exception e) {
            setMessage("解析返回参数错误");
            return false;
        }
        if (!mapFromXML.get("return_code").equals("SUCCESS")) {
            setMessage(mapFromXML.get("return_msg"));
            return false;
        }
        if (mapFromXML.get("result_code") != null && mapFromXML.get("result_code").equals("SUCCESS")) {
            return true;
        }
        setMessage(mapFromXML.get("err_code_des"));
        return false;
    }

}
